//
//  LSYBaseNavigationController.swift
//  LSYWeibo
//
//  Created by 刘诗岩 on 16/4/11.
//  Copyright © 2016年 刘诗岩. All rights reserved.
//

import UIKit

class LSYBaseNavigationController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        // 设置导航条背景图片
        navigationBar.setBackgroundImage(UIImage(named: "navbarbackimaegg"), forBarMetrics: .Default)
        
        self.interactivePopGestureRecognizer?.delegate = self
    }
    
    /** 第二个控制器返回是根控制器 title, 剩下的都是'返回' */
    // 重写不 super
    override func pushViewController(viewController: UIViewController, animated: Bool) {
        
        if childViewControllers.count > 0 {
            
            var backTitle = "返回"
            
            if childViewControllers.count == 1 {
                
                backTitle = childViewControllers.first?.title ?? ""
            }
            
            
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_back_withtext", title: backTitle, target: self, action: "back")

            //隐藏底部tabbar
            viewController.hidesBottomBarWhenPushed = true
            
            //设置颜色  页面跳转的时候会有颜色切换 导致卡顿
            viewController.view.backgroundColor = UIColor.whiteColor()
        }
        
        super.pushViewController(viewController, animated: animated)
        print(childViewControllers.count)
    }
    
    @objc private func back(){
        
        popViewControllerAnimated(true);
    }
    
    /** 返回 true 可用, 返回 false 禁用 */
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {

        return childViewControllers.count != 1

    }

}
